<template>
	<view>
		<view class="bgFill"></view>
		<view class="infoBox flex align-center flex-direction" v-if="datas">
			<image class="shopLogo" :src="datas.store.cover_url"></image>
			<text class="shopName">{{datas.store.name}}</text>
			<view class="shopclass flex align-center" style="margin-top: 20upx;">
				<text>{{datas.category.name}}</text>
				<text style="margin: 0 20upx;">|</text>
				<text>销量{{datas.sales_num}}</text>
			</view>
			<view class="flex align-center" style="margin-top: 20upx;">
				<view class="timeText">营业时间</view>
				<view class="timeInfo flex align-center justify-center">
					<block v-if="datas.store.is_all_day == '0'">
						<text>9:00</text>
						<text style="margin: 0 6upx;">~</text>
						<text>21:00</text>
					</block>
					<text v-else>24小时营业</text>
				</view>
			</view>
			<view class="addressInfo flex align-center justify-between">
				<image src="/static/image/WB/address.png"></image>
				<view style="width: 400upx;">{{datas.store.address}}
					<view>{{datas.circle.name}}</view>
				</view>
			</view>
			<view class="members flex align-center">
				<view>{{datas.fans}}粉丝</view>
				<text>|</text>
				<view>{{datas.views}}人气</view>
			</view>
		</view>
		<view class="elseBox">
			<view class="elseTitle flex align-center justify-between">
				<text>消费金额</text>
				<view>
					<text style="font-size: 24upx;">美试券余额：</text>
					<text style="font-weight: bold;color:#ff8959;">{{balance}}</text>
				</view>

			</view>
			<view class="inputBox flex align-center">
				<text>￥</text>
				<input type="number" placeholder-style="font-size:30upx" placeholder="请与店员确认后输入消费金额" v-model="money" />
			</view>
			<view class="moneys">
				<text>美试券：{{intergral}}</text>
				<text style="margin-left: 100upx;">现金：{{Math.round((money-intergral)*100)/100}}</text>
			</view>
		</view>
		<view class="buyBtn" v-if="showBtn" @click="toPay">立即买单</view>
	</view>
</template>

<script>
	import {
		post,
		abortPost,
		get
	} from '@/utils/post.js'
	export default {
		components: {

		},
		data() {
			return {
				showBtn: true,
				datas: '',
				config: '',
				balance: '',
				rate: '',
				money: ''
			};
		},
		onLoad(op) {
			this.id = op.id
			this.loadData()
			this.loadConf()
		},
		computed: {
			intergral() {
				var num = 0
				if (this.money) {
					var n = Math.round(this.money * this.rate*100)/100
					if (n > this.balance) {
						num = this.balance
					} else {
						num = n
					}
				}
				return num
			}
			
		},
		methods: {
			loadData() {
				get(this.$api.nearby.detail, {
					id: this.id
				}, false, res => {
					this.datas = res.data
				})
			},
			loadConf() {
				get(this.$api.nearby.pay_conf, {
					mch_id: this.id
				}, true, res => {
					this.balance = res.data.balance - 0
					this.rate = res.data.rate
				})
			},
			toPay() {
				var data = {
					mch_id: this.id,
					money: this.money,
					type: 2
				}
				post(this.$api.nearby.pay, data, true, res => {
					setTimeout(() => {
						if (this.$wx && this.$wx.isWechat()) { //检查是否是微信环境
							this.$wx.wxpay(res.data, (wxres) => {
								//支付成功
								if (wxres == 1) {
									this.$showM('支付成功', false, res => {
										this.money = ''
										this.loadConf()
										this.loadData()
									})
								} else {
									this.$showM('取消支付', false)
								}
							})
						}
					}, 50)
				})
			},
			tomap(){
				if(this.datas.store.longitude){
					this.$config.weburl = `https://apis.map.qq.com/tools/poimarker?type=0&marker=coord:${this.datas.store.latitude},${this.datas.store.longitude};title:${this.datas.store.poiaddress || ''};addr:${this.datas.store.address}&key=NMKBZ-NN3KR-OYQWQ-WPSXT-6SX6V-HIFFU&referer=msk`
					uni.navigateTo({
						url:'/pages/web2/web'
					})
				}
			}
		}
	}
</script>

<style scoped>
	.buyBtn {
		width: 700upx;
		margin: 100upx auto 0;
		line-height: 70upx;
		text-align: center;
		color: #fff;
		font-size: 30upx;
		background-image: linear-gradient(to right, #ffd171, #ff8959);
		border-radius: 10upx;
	}

	.moneys {
		font-size: 30upx;
		color: #888;
		margin-top: 30upx;
	}

	.inputBox {
		margin-top: 30upx;
		font-size: 34upx;
	}

	.inputBox input {
		font-size: 40upx;
		height: 60upx;
		line-height: 60upx;
		padding-left: 20upx;
		border-bottom: 1px solid #ccc;
		flex-grow: 1;
	}

	.elseTitle {
		font-size: 30upx;
		padding-left: 20upx;
		border-left: 4px solid #ff9853;
		line-height: 34upx;
	}

	.elseBox {
		width: 700upx;
		padding: 30upx;
		background: #fff;
		border-radius: 20upx;
		margin: 20upx auto 0;
	}

	.members {
		width: 100%;
		border-top: 1px solid #eee;
		padding-top: 20upx;
		margin-top: 20upx;
		font-size: 24upx;
		color: #aaa;
	}

	.members view {
		flex: 1;
		text-align: center;
	}

	.addressInfo {
		width: 460upx;
		font-size: 24upx;
		margin-top: 20upx;
	}

	.addressInfo image {
		width: 40upx;
		height: 40upx;
	}

	.timeInfo {
		font-size: 22upx;
		width: 180upx;
		color: #ff9853;
		border: 1px solid #ff9853;
		height: 40upx;
	}

	.timeText {
		line-height: 40upx;
		width: 130upx;
		background: #ff9853;
		color: #fff;
		font-size: 22upx;
		text-align: center;
	}

	.shopclass {
		font-size: 24upx;
		color: #aaa;
	}

	.shopName {
		font-size: 30upx;
		font-weight: bold;
		line-height: 60upx;
	}

	.shopLogo {
		width: 140upx;
		height: 140upx;
		border-radius: 50%;
		margin-top: -90upx;
	}

	.infoBox {
		width: 700upx;
		padding: 20upx 40upx;
		margin: 0 auto 0;
		background: #fff;
		border-radius: 10upx;
		box-shadow: 0 5px 5px #eee;
	}

	.bgFill {
		width: 100%;
		height: 100upx;
	}

	page {
		background: #f5f5f5;
	}
</style>
